package com.kindnesslibrary.network.callback;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.kindnesslibrary.network.HttpConfig;

import java.io.Serializable;

/**
 * 请求处理
 * Created by chenhongxin on 2017/5/7.
 */
public abstract class BaseCallBack implements Runnable {

    public static final String DATA_KEY = "data";

    private String url; // 请求路径
    private Object content; // 参数内容
    private Handler handler; // 界面提交的Hnadler
    public boolean isRemove; // 是否被取消

    public BaseCallBack(String url, Object content, Handler handler){
        this.url = url;
        this.content = content;
        this.handler = handler;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public Object getContent() {
        return content;
    }

    @Override
    public void run() {
        if(isRemove){
            if(handler != null){
                sendMessage(HttpConfig.getInstance().MSG_CANCEL, 0, null);
            }
            return;
        }
        runing();
    }

    protected void sendMessage(int what,
                            int arg1,
                            Object obj){
        if (handler != null) {
            Message msg = handler.obtainMessage();
            msg.what = what;
            msg.arg1 = arg1;
            Bundle bundle = new Bundle();
            bundle.putSerializable(DATA_KEY, (Serializable) obj);
            msg.setData(bundle);
            if (handler != null)
                handler.sendMessage(msg);
        }
    }

    public abstract void runing();

}
